define(['angularAMD'], function (angularAMD) {
    angularAMD.service('BTPrinterSetting', ['$rootScope', '$filter', '$translate', 'config', '$timeout', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $timeout, $MMWService, $mdDialog){
            function failedCallBack(error){
                console.log(error);
            }

            this.init = function (options) {
                function openDialog(settings) {
                    $mdDialog.dialog(
                        'module/system/program/BTPrinterSetting/BTPrinterSetting.html',
                        function(dialog){
                            return {
                                css_url:'module/system/program/BTPrinterSetting/BTPrinterSetting.css',
                                settings:settings,
                                selectPrinter:function(){
                                    printerHelper.getDeviceList(function(deviceList){
                                        $rootScope.showSelect({
                                            label : 'name',
                                            code : 'uuid',
                                            selectCode : '',
                                            list : deviceList,
                                            confirm : function(selectItem, radio_dialog){
                                                radio_dialog.hide();
                                                dialog.settings.printer_name = selectItem.name;
                                                dialog.settings.printer_uuid = selectItem.uuid;
                                            }
                                        });
                                    }, failedCallBack);
                                },
                                back: function () {
                                    dialog.hide();
                                },
                                confirm: function () {
                                    if(!dialog.settings.printer_name){
                                        $rootScope.showAlert($filter('translate')('BTPrinterSetting.msg.not_select_btprinter'));
                                    } else {
                                        var _settings = dialog.settings;
                                        localStorage.setItem(config.appName + '_btprinter_settings', JSON.stringify(_settings));
                                        dialog.hide();
                                        if(options.callback){
                                            options.callback(_settings);
                                        }
                                    }
                                }
                            };
                        }
                    );
                }
                (function(){
                    var _btprinter_settings = localStorage.getItem(config.appName + '_btprinter_settings');
                    if(_btprinter_settings){
                        _btprinter_settings = JSON.parse(_btprinter_settings);
                    } else {
                        _btprinter_settings = {
                            printer_name:'',
                            printer_uuid:'',
                            printer_width:78,
                            printer_feed:'N'
                        };
                    }
                    openDialog(_btprinter_settings);
                })();
            };
        }]);
});
